import rcontrolutil
import rcontrol
import Tkinter

class RImage(rcontrol.RControl):
	"""
	Image control.
	
	Properties:
	*bgcolor* - Background color of the label. RGB triplet, such as (255,0,0) for pure red.
	*enabled* - If False, the label is grayed out.
	*width* - Set width of input, where one unit is the width of a char. If set to 0, width is based on contents (the default behavior).
	*height* - Set height of input, by the height of a line of text. If set to 0, width is based on contents (the default behavior).
	*borderwidth* - An integer, width of border in pixels.
	*borderstyle* - Border style. One of 'sunken','raised','groove','ridge','flat'. Default is 'sunken'.
	*anchor* - Set where in the button the text (or image) should be located. Provide one of the strings 'center','top','topright','right','bottomright','bottom','bottomleft','left','topleft'. Default is center.
	
	"""
	
	
	def __init__(self,  parent=None, **options):
		self._newWidgetManual(parent, Tkinter.Label)
		
		self.set_many(**options)
	
	def property_enabled_get(self):
		if self._basecontrol['state']==Tkinter.DISABLED: return False
		else: return True
	def property_enabled_set(self, value):
		if value: self._basecontrol['state'] = Tkinter.NORMAL
		else: self._basecontrol['state'] = Tkinter.DISABLED
	
	def set_image(self, filename):
		if not filename:
			#show no image
			self._basecontrol.image = None
			self._basecontrol.config(image='')
		else:
			fnamelower = filename.lower()
			if not (fnamelower.endswith('.gif') or fnamelower.endswith('.pgm') or fnamelower.endswith('.ppm')):
				raise rcontrolutil.RException("Only images of format .gif, .pgm, or .ppm are supported.")
			image = Tkinter.PhotoImage(file=filename)
			self._basecontrol.image = image #store a reference to image! important!
			self._basecontrol.config(image=image)
		
	def set_image_base64(self, data):
		'''data must be a base-64 encoded GIF'''
		if not data:
			#show no image
			self._basecontrol.image = None
			self._basecontrol.config(image='')
		else:
			image = Tkinter.PhotoImage(format='gif', data=data)
			self._basecontrol.image = image #store a reference to image! important!
			self._basecontrol.config(image=image)
	
		
	
	_properties = {
	
		'enabled':(property_enabled_get, property_enabled_set),
		
		'bgcolor': ('background',rcontrolutil.mapColor),
		'width': 'width',
		'height': 'height',
		'borderwidth': 'borderwidth',
		
		'borderstyle': ('relief',rcontrolutil.mapBorderstyle),
		'anchor': ('anchor', rcontrolutil.mapAnchor),
		}
	



